#include <QTableWidgetItem>
#include"Table.h"

Table::Table(QWidget *parent):QTableWidget(parent),colSelector(" "),rowSelector("\n")
{
    sortByColumn(0);  
}

void Table::setSize(const int &rowsCount, const int &colsCount)
{
   if((rowsCount>=0)&&(colsCount>=0))
   {
        while(rowCount()>rowsCount) removeRow(rowCount()-1);
        while(columnCount()>colsCount) removeColumn(columnCount()-1);
        if((rowCount()<rowsCount)||(columnCount()<colsCount))
        {
            for(int needRCount = rowsCount -rowCount();needRCount>0;needRCount--)
                addRow();
            for(int needCCount = colsCount-columnCount();needCCount>0;needCCount--)
               addColumn();
        }
        emit sizeChange();
    }
}

void Table::setCell(const int &row, const int &col,const  QString &text)
{
    if((row>=0)&&(row<rowCount())&&(col>=0)&&(col<columnCount()))
    {
         item(row,col)->setText(text);
    }
}

void Table::setCell(const int &row, const int &col, const QVariant &data)
{
    if((row>=0)&&(row<rowCount())&&(col>=0)&&(col<columnCount()))
    {
         item(row,col)->setData(0,data);
    }
}

void Table::setCell(const int &row, const int &col,const  QString &text, const QIcon&icon)
{
    if((row>=0)&&(row<rowCount())&&(col>=0)&&(col<columnCount()))
    {
         item(row,col)->setText(text);
         item(row,col)->setIcon(icon);
    }
}

void Table::addColumn()
{
    setColumnCount(columnCount()+1);
    for(int i=0;i<rowCount();i++)
    {
        QTableWidgetItem *item = new QTableWidgetItem("");
        setItem(i,columnCount()-1,item);
    }
}

void Table::addRow()
{
    setRowCount(rowCount()+1);
    for(int i=0;i<columnCount();i++)
    {
        QTableWidgetItem *item = new QTableWidgetItem("");
        setItem(rowCount()-1,i,item);
    }
}

void Table::copy()
{
    QTableWidgetSelectionRange range = selectedRange();
    QString data;
    for(int i=0; i<range.rowCount();i++)
    {
        if(i>0) data+=rowSelector;
        for(int j=0;j<range.columnCount();j++)
        {
            if(j>0) data+=colSelector;
            data+= this->item(range.topRow()+i,range.leftColumn()+j)->data(Qt::DisplayRole).toString();
        }
    }
    QApplication::clipboard()->setText(data);
}

QTableWidgetSelectionRange Table::selectedRange()
{
    QList<QTableWidgetSelectionRange> range = selectedRanges();
    if(range.isEmpty())
        return QTableWidgetSelectionRange();
    return range.first();
}

void Table::setColSelector(const QString &selector)
{
    colSelector=selector;
}

void Table::setRowSelector(const QString &selector)
{
    rowSelector=selector;
}

QString Table::RowSelector() const
{
    return rowSelector;
}

QString Table::ColSelector() const
{
    return colSelector;
}

void Table::resizeEvent(QResizeEvent *event)
{
    QVector<int> x=newColWidth(event->size());
    for(int i=0;i<x.count();i++) setColumnWidth(i,x.at(i));
}

void Table::setColSettings(const QString &settings)
{
    QStringList Setting = settings.split(";");
    if(columnCount()==Setting.count())
    {
        colSetting.clear();
        colSetting=Setting;
    }
}

QVector<int> Table::newColWidth(const QSize&size) const
{
    QVector<int> neww;
    QRegExp reg("(\\d+)%");
    for(int i=0;i<colSetting.count();i++)
    {
        QString x = colSetting.at(i);
        if(reg.indexIn(x)!=-1)
        {
            x=x.replace("%","");
            neww.append(trunc((size.width()-5)*x.toInt()/100));
        }
        else
            neww.append(x.toInt());
    }
    return neww;
}


